package gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JPanel;

import app.Conversion;
import app.Ingredient;
import app.Measurement;
import net.miginfocom.swing.MigLayout;

/********************************************************
 *
 *  Project :  TeamProject
 *  Package :  gui
 *  File    :  IngredientPanel.java
 *  Name    :  Tyler Daines
 *  Date    :  Nov 12, 2013
 *
 ********************************************************/
public class IngredientPanel extends JPanel
{
	private ArrayList<Ingredient> ingredients;
	private ArrayList<IngredientFieldPanel> ingredientFields;
	
	private JButton convertToButton;
	
	/********************************************************
	 * Name       : IngredientPanel
	 * Purpose    : Constructor
	 * Parameters : 
	 ********************************************************/
	public IngredientPanel(ArrayList<Ingredient> ingredients)
	{
		setLayout(new MigLayout("gap 0 0"));
		
		this.ingredients = ingredients;
	
		initButtons();
		initIngredients();
		
	}

	/********************************************************
	* Name       : initButtons
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initButtons()
	{
		ButtonListener buttonListener = new ButtonListener();
		
		
		convertToButton = new JButton("Convert To");
		convertToButton.addActionListener(buttonListener);
		add(convertToButton,"wrap");
	}

	/********************************************************
	* Name       : initIngredients
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	private void initIngredients()
	{
		ingredientFields = new ArrayList<>();
		
		if(ingredients.size() > 0)
		{
			for(Ingredient ele : ingredients)
			{
				addIngredientField(ele.getName(), ele.getAmount(), ele.getMeasurement());
			}
		}
		else 
		{
//			addNewIngredientField("", (float) 0, Measurement.CUP);
		}
		
	}
	

	
	/********************************************************
	* Name       : addIngredientField
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	public void addIngredientField(String name, Float amount, Measurement measurement)
	{
		
		ingredientFields.add(new IngredientFieldPanel(name, amount, measurement));
		this.add(ingredientFields.get(ingredientFields.size()-1), "wrap");
		this.revalidate();		
	}
	
	/********************************************************
	* Name       : addNewIngredientField
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	public void addNewIngredientField(String name, Float amount, Measurement measurement)
	{
		ingredients.add(new Ingredient(name, amount, measurement));
		ingredientFields.add(new IngredientFieldPanel(name, amount, measurement));
		this.add(ingredientFields.get(ingredientFields.size()-1), "wrap");
		this.revalidate();	
	}
	
	/********************************************************
	* Name       : removeIngredientField
	* Purpose    : 
	* Parameters : 
	* Returns    : void
	********************************************************/
	public void removeIngredientField()
	{
		if(ingredients.size() > 0 && ingredientFields.size() > 0 )
		{
			Iterator<Ingredient> iterIng = ingredients.iterator();
			Iterator<IngredientFieldPanel> iterField = ingredientFields.iterator();
			while(iterIng.hasNext() && iterField.hasNext())
			{
				Ingredient ingredient = (Ingredient) iterIng.next();
				IngredientFieldPanel fieldPanel = (IngredientFieldPanel) iterField.next();
				
				if(fieldPanel.getIngredientCheckBox().isSelected())
				{
					this.remove(fieldPanel);
					iterIng.remove();
					iterField.remove();
				}
				
			}
					
			this.revalidate();
		}
		
	}
	
	public void saveIngredients() throws Exception
	{
		for(int i = 0; i < ingredientFields.size(); i++)
		{
			Ingredient ingredient = ingredients.get(i);
			IngredientFieldPanel ingredientFieldPanel = ingredientFields.get(i);

			ingredient.setAmount(ingredientFieldPanel.getAmount());
			ingredient.setMeasurement(ingredientFieldPanel.getMeasurement());
			ingredient.setName(ingredientFieldPanel.getName());		
			
			
		}
	}
	

	public ArrayList<Ingredient> getIngredients()
	{
		return ingredients;
	}
	
	private class ButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent e)
		{
			if(e.getSource() == convertToButton)
			{
				for ( int i = 0; i < ingredientFields.size(); i++ )
				{
					Conversion conversionerer = new Conversion();
					IngredientFieldPanel ingredientFieldPanel = ingredientFields.get(i);
					Ingredient tempIngredient = ingredients.get(i);
					float amountToConvert = tempIngredient.getAmount();
					Measurement fromMeasure = tempIngredient.getMeasurement();
					Measurement toMeasure = ingredientFieldPanel.getMeasurement();
					Float result = (amountToConvert*conversionerer.getAmountByMeasure(fromMeasure))/conversionerer.getAmountByMeasure(toMeasure);
					tempIngredient.setAmount(result);
					tempIngredient.setMeasurement(toMeasure);
					ingredientFieldPanel.setAmount(result);
				}

			}
		}
	}
}
